package com.hu.ta.assignment5.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class NGram implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2715271743688125627L;

	public static final int NGRAM_SIZE = 1;

	public String[] poses;

	// TODO löschen und NGRAM_SIZE abändern
	public NGram(String tag) {
		List<String> in = new ArrayList<String>(NGRAM_SIZE);
		in.add(tag);

		// Wenn liste zu kurz, dann füge leere elemente am Anfang an
		for (int i = 0; i < NGRAM_SIZE - in.size(); i++) {
			in.add(0, "");
		}

		this.poses = new String[in.size()];
		this.poses = in.toArray(this.poses);
	}

	public NGram(List<String> in) {
		// Wenn liste zu kurz, dann fuege leere elemente am Anfang an
		for (int i = 0; i < NGRAM_SIZE - in.size(); i++) {
			in.add(0, "");
		}

		this.poses = new String[in.size()];
		this.poses = in.toArray(this.poses);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof NGram) {
			NGram g = (NGram) obj;
			int i = 0;
			for (String a : g.poses) {
				if (!a.equals(poses[i++])) {
					return false;
				}
			}
			return true;
		} else {
			throw new IllegalArgumentException();
		}
	}

	@Override
	public int hashCode() {
		int ret = 0;
		for (String str : poses) {
			ret += str.hashCode();
		}

		return new Integer(ret).hashCode();
	}

	public NGram getNextNGram(String newPOS) {
		List<String> ret = new ArrayList<String>(NGRAM_SIZE);
		for (int i = 1; i < NGRAM_SIZE; i++) {
			ret.add(this.poses[i]);
		}
		ret.add(newPOS);
		return new NGram(ret);
	}

	@Override
	public String toString() {
		String ret = "";
		for (String pos : poses) {
			ret += pos + "\t";
		}
		return ret;
	}
}
